<template>
	<scroll-view :scroll-y="true">
		<successPage v-show="successPageType" ref="successPage" v-bind:successPageMoney="successPageMoney"></successPage>
		<view class="withdrawalInfoBox">
			<view>提现到</view>
			<view class="bankCard">{{cardData.bankname}} {{cardData.bankcardnum}}</view>
			<view>预计下个工作日到账</view>
		</view>

		<view class="withdrawalBox">
			<view class="withdrawalTips">提现金额</view>
			<view class="inputBox">
				<text style="font-size:26px;margin-right:20rpx;margin-top:-5px;">￥</text>

				<uni-easyinput :clearable="false" @input="changeInput" :value="withdrawalValue" :inputBorder="false"
					:focus="true" type="digit">
				</uni-easyinput>
			</view>
			<view class="allWithdrawal">
				当前可提现：<text class="font_weight">{{cash_amount}}</text>
				<text class="allWithdrawalBtn" @click="allWithdrawalBtn()">全部提现</text>
			</view>
		</view>
		
		<view class="withdrawalBox">
			<view class="withdrawalList" @click="extractRecordBtn(1)">
				<text>提现记录</text>
				<image src="https://test.jhwl.site/static/img/arrowright.png"></image>
			</view>
		</view>
		<view class="confirmS lr_spacing" v-show="confirmType == true" @click="confirmBtn()">确认提现</view>
		<view class="confirm lr_spacing" v-show="confirmType == false">确认提现</view>
		<view class="layer" v-show="layerType == true"></view>
		<view class="layerBox" v-show="layerType == true">
			<view class="shopTitle">提现密码</view>
			<view class="inputBoxWd">
				<input type="text" class="input" placeholder="请输入提现密码" placeholder-style="font-size:28rpx;color:#9e9e9e" @input="onKeyInput" name="repaypwd" :value="inputValue" password="true"></input>
			</view>
			<view class="operateBox">
				<text class="operateBoxCancel" @click="layerType = false">取消</text>
				<text class="operateConfirm" @click="confirmBtnS()">确认</text>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	var app = getApp();
	import successPage from '../utils/successPage.vue';
	import person from '../utils/person.js';
	export default {
		components: {
			successPage,
		},
		data() {
			return {
				successPageType:false,//提现完成页面
				successPageMoney:'0.00',//提现完成页面金额
				layerType:false,
				loading: false,
				withdrawalValue: '',
				confirmType: false, //确认提现按钮状态false:不可点击
				webViewType: false,
				cardData: {}, //银行卡data
				cash_amount: '0.00', //可体现金额
				pageType:'1',//页面状态1:合伙人 2:商家
				inputValue:'',//密码value
			}
		},

		mounted() {
			this.pageType = uni.getStorageSync('pageType');
			console.log(this.pageType);
			this.getCard();
		},

		methods: {
			getCard() {
				let _this = this;
				_this.loading = true;
				app.get('ApiMy/set', {}, function(data) {
					_this.cardData = data.userinfo;
				});
				_this.getStoreInfo(_this);
			},

			//查询可提现金额
			getStoreInfo(_this) {
				let ajaxUrl = '';
				if(_this.pageType == '1'){
					ajaxUrl = 'ApiStore/getParnerInfo';
				}else if(_this.pageType == '2'){
					ajaxUrl = 'ApiStore/getStoreInfo';
				}else{
					ajaxUrl = 'ApiStore/getBranchCompanyInfo';
				}
				app.get(ajaxUrl, {
					page: 1,
					limit: 10
				}, function(res) {
					_this.cash_amount = res.cash_amount;
					_this.loading = false;
				});
			},

			//全部提现按钮
			allWithdrawalBtn() {
				this.withdrawalValue = '';
				this.withdrawalValue = this.cash_amount;
				this.confirmType = true;
			},

			//输入框触发事件
			changeInput(e) {
				this.withdrawalValue = e;
				if (e <= parseFloat(this.cash_amount) && this.cash_amount > 0) {
					this.confirmType = true;
				} else {
					this.confirmType = false;
				}
			},
			
			//密码输入框
			onKeyInput(event){
				this.inputValue = event.target.value;
				console.log(this.inputValue);
			},
			
			//提现记录事件
			extractRecordBtn(index){
				app.goto('/pagesZ/extractRecord/extractRecord');
			},
			
			//验证密码
			confirmBtnS(){
				uni.showLoading({
					title: '加载中'
				});
				let _this = this;
				app.post('ApiStore/checkpaypwd', {
					password: _this.inputValue,
				}, function(res) {
					if(res.msg == 'success'){
						let ajaxUrl = '';
						if(_this.pageType == '1'){
							ajaxUrl = 'ApiAgent/commissionWithdraw';
						}else{
							ajaxUrl = 'ApiStore/bwithdraw';
						}
						app.post(ajaxUrl, {
							money: _this.withdrawalValue,
							paytype: '银行卡'
						}, function(data) {
							if (data.status == 0) {
								uni.hideLoading()
								app.error(data.msg);
								return;
							} else {
								setTimeout(function() {
									uni.hideLoading()
									_this.successPageType = true; //打开提现成功弹窗
									_this.successPageMoney = parseFloat(_this.withdrawalValue).toFixed(2); //提现成功弹窗金额
									person.shopData.amount = (person.shopData.amount - _this.withdrawalValue).toFixed(2);//余额 
									person.shopData.Kamount = (person.shopData.Kamount - _this.withdrawalValue).toFixed(2);//可体现金额
								}, 2000);
								
							}
						});	
					}else{
						app.error('密码错误');
						_this.inputValue = '';
					}
				});	
			},

			//提现按钮
			confirmBtn() {
				let _this = this;
				//验证是否有密码
				app.post('ApiStore/checkpaypwd', {
					password:'',
				}, function(res) {
					if(res.msg == 'success'){
						uni.showModal({
							title: '操作提示',
							content: '暂无交易密码,点击确定前往设置密码！',
							success: function(res) {
								if (res.confirm) {
									app.goto('/pagesExt/my/paypwd');
								}
							}
						});
					}else{
						_this.layerType = true;
					}
				});	
			},

		},
	}
</script>


<style scoped lang="scss">
	.layer {
		width: 100%;
		height: 100vh;
		float: left;
		position: fixed;
		top: 0;
		left: 0;
		background-color: #000;
		opacity: 0.6;
		z-index: 1000;
	}
	
	.operateBox{
		width:100%;
		float:left;
		font-weight: bold;
		margin:20rpx 0 30rpx 0;
	}
	
	.operateBoxCancel{
		line-height: 70rpx;
		background-color: #bfbfbf;
		color:#000;
		width:40%;
		text-align: center;
		border-radius: 15rpx;
		float:left;
		margin-left:5%;
	}
	
	.operateConfirm{
		width:40%;
		float:left;
		margin-left:5%;
		text-align: center;
		line-height: 70rpx;
		border-radius: 15rpx;
		background-color: #01B681;
		color:#fff;
	}
	
	.inputBoxWd{
		width:60%;
		float:left;
		margin:0 20% 20rpx 20%;
	}
	
	.layerBox{
		width: 80%;
		float:left;
		background-color: #fff;
		z-index: 1001;
		position: fixed;
		padding:20rpx;
		top:50%;
		transform: translate(0, -50%); 
		border-radius: 15rpx;
		left:10%;
	}
	
	.shopTitle {
		width: 100%;
		float: left;
		text-align: center;
		font-weight: bold;
		font-size: 30rpx;
		color: #868686;
		line-height: 35px;
		margin-bottom: 20rpx;
	}
	
	page {
		background-color: #f9f9f9;
	}

	.position {
		width: 90%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		padding: 20rpx 5%;
		line-height: 60rpx;
		font-size: 15px;
		font-weight: bold;
		/* position: fixed; */
		z-index: 99;
	}

	.webView {
		width: 100%;
		float: left;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
	}

	.withdrawalInfoBox {
		width: 90%;
		float: left;
		margin: 30rpx 5% 0 5%;
		color: #666666;
	}

	.bankCard {
		font-weight: bold;
		margin: 30rpx 0 15rpx 0;
		font-size: 28rpx;
		color: #000;
	}

	.withdrawalBox {
		width: 94%;
		float: left;
		margin: 20rpx 3%;
		padding: 25rpx 3%;
		background-color: #fff;
		border-radius: 7px;
	}
	
	.withdrawalList{
		width:100%;
		display: flex;
		align-items:center;
		justify-content:space-between;
	}
	
	.withdrawalList image{
		width:40rpx;
		height:40rpx;
	}

	.withdrawalTips {
		width: 96%;
		padding: 0 2%;
		float: left;
		font-size: 26rpx;
		font-weight: bold;
	}

	.inputBox {
		width: 96%;
		margin: 0 2%;
		padding: 20rpx 0 10rpx 0;
		display: flex;
		font-size: 35rpx;
		align-items: baseline;
		border-bottom: 1px solid #EEEEEE;
	}

	.allWithdrawal {
		width: 96%;
		padding: 0 2%;
		margin-top: 30rpx;
		margin-bottom: 10rpx;
	}

	.allWithdrawalBtn {
		margin-left: 25rpx;
		color: #0597E7;
		border-bottom: 1px solid #0597E7;
	}

	.confirm {
		width: 94%;
		float: left;
		position: fixed;
		left: 3%;
		bottom: 20rpx;
		text-align: center;
		line-height: 90rpx;
		color: #fff;
		font-weight: bold;
		font-size: 28rpx;
		border-radius: 5px;
		background-color: #CCCCCC;
	}

	.confirmS {
		width: 94%;
		float: left;
		position: fixed;
		left: 3%;
		bottom: 20rpx;
		text-align: center;
		line-height: 90rpx;
		color: #fff;
		font-weight: bold;
		font-size: 28rpx;
		border-radius: 5px;
		background-color: #01B681;
	}

	::v-deep .inputBox .u-border {
		border: none;
		font-weight: bold;
		margin-top: 10rpx;
		padding: 0 !important;
	}
</style>